package com.lyl.momanapp.face.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.util.Log;

import com.lyl.momanapp.face.entities.FaceRect;

import java.io.ByteArrayOutputStream;



public class ImageCropUtil {
    public static Bitmap cropImg(FaceRect thisFace, Camera thisCamera, int preViewWidth, int preViewHeight, byte[] cropImgBuffer) {
        //处理data
        Camera.Size previewSize = thisCamera.getParameters().getPreviewSize();//获取尺寸,格式转换的时候要用到

        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;

        YuvImage yuvimage = new YuvImage(
                cropImgBuffer,
                ImageFormat.NV21,
                previewSize.width,
                previewSize.height,
                null);

        Rect rect = new Rect(0, 0, previewSize.width, previewSize.height);
        if (rect.contains(thisFace.bound)) {
            Log.e("TAG", "inside");  //暂时只有前置摄像头可用

            //从yuv复制一份原图
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            yuvimage.compressToJpeg(new Rect(0, 0, preViewWidth, preViewHeight), 100, baos);
            byte[] rawImage = baos.toByteArray();

            //将rawImage转换成bitmap
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap rotatedBitmap = FaceUtil.rotateImage(270, BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options));
            Bitmap reversedBitmap = FaceUtil.reverseBitmap(rotatedBitmap, 0);

            return Bitmap.createBitmap(reversedBitmap, thisFace.bound.left, thisFace.bound.top, thisFace.bound.width(), thisFace.bound.height(), null, false);
        } else {
            Log.e("TAG", "not inside");
        }
        return null;
    }

    public static Bitmap getBitmapFromBuffer(boolean isfront, Camera thisCamera, int preViewWidth, int preViewHeight, byte[] cropImgBuffer) {
        //处理data
        Camera.Size previewSize = thisCamera.getParameters().getPreviewSize();//获取尺寸,格式转换的时候要用到

        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;

        YuvImage yuvimage = new YuvImage(
                cropImgBuffer,
                ImageFormat.NV21,
                previewSize.width,
                previewSize.height,
                null);

        //从yuv复制一份原图
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(0, 0, preViewWidth, preViewHeight), 100, baos);
        byte[] rawImage = baos.toByteArray();

        //将rawImage转换成bitmap
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap rotatedBitmap;
        if (isfront) {
            rotatedBitmap = FaceUtil.rotateImage(270, BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options));
            return FaceUtil.reverseBitmap(rotatedBitmap, 0);
        } else {
            return FaceUtil.rotateImage(90, BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options));
        }

    }

    public static Bitmap cropImg(Rect thisFace, Camera thisCamera, int preViewWidth, int preViewHeight, byte[] cropImgBuffer) {
        //处理data
        Camera.Size previewSize = thisCamera.getParameters().getPreviewSize();//获取尺寸,格式转换的时候要用到

        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;

        YuvImage yuvimage = new YuvImage(
                cropImgBuffer,
                ImageFormat.NV21,
                previewSize.width,
                previewSize.height,
                null);

        Rect rect = new Rect(0, 0, previewSize.width, previewSize.height);
        if (rect.contains(thisFace)) {
            Log.e("TAG", "inside");  //暂时只有前置摄像头可用

            //从yuv复制一份原图
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            yuvimage.compressToJpeg(new Rect(0, 0, preViewWidth, preViewHeight), 100, baos);
            byte[] rawImage = baos.toByteArray();

            //将rawImage转换成bitmap
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap rotatedBitmap = FaceUtil.rotateImage(270, BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options));
            Bitmap reversedBitmap = FaceUtil.reverseBitmap(rotatedBitmap, 0);

            // return Bitmap.createBitmap(reversedBitmap, thisFace.left, thisFace.top, thisFace.width(), thisFace.height(), null, false);
            return reversedBitmap;
        } else {
            Log.e("TAG", "not inside");
        }
        return null;
    }
}
